package huzh.foundation.util;

/**
 *<p>title:Base64O</p>
 *<p>description:</p>
 * @since 2014-6-24
 * @author huzh
 * @version 1.0
 */
public class Base64O {
    private static final char[] enc_table = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
    private static final byte[] dec_table = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };

    public static byte[] decode(byte[] paramArrayOfByte)
    {
      int i = 0;
      int k = paramArrayOfByte.length;
      int m = 0;
      for (int j = k - 1; j >= 0; j--)
      {
        if (paramArrayOfByte[j] > 32)
          m++;
        if (paramArrayOfByte[j] != 61)
          continue;
        i++;
      }
      if (m % 4 != 0)
        throw new IllegalArgumentException("Length not a multiple of 4");
      int n = m / 4 * 3 - i;
      byte[] arrayOfByte1 = new byte[n];
      int j = 0;
      byte[] arrayOfByte2 = new byte[4];
      int i1 = 0;
      int i2 = 0;
      int tmp108_107 = (arrayOfByte2[2] = arrayOfByte2[3] = 61);
      arrayOfByte2[1] = (byte) tmp108_107;
      arrayOfByte2[0] = (byte) tmp108_107;
      while (j < k)
      {
        int i3 = paramArrayOfByte[(j++)];
        if (i3 > 32)
          arrayOfByte2[(i2++)] = (byte) i3;
        if (i2 != 4)
          continue;
        i1 += decode(arrayOfByte1, i1, arrayOfByte2[0], arrayOfByte2[1], arrayOfByte2[2], arrayOfByte2[3]);
        i2 = 0;
        int tmp196_195 = (arrayOfByte2[2] = arrayOfByte2[3] = 61);
        arrayOfByte2[1] = (byte) tmp196_195;
        arrayOfByte2[0] = (byte) tmp196_195;
      }
      if (i2 > 0)
        decode(arrayOfByte1, i1, arrayOfByte2[0], arrayOfByte2[1], arrayOfByte2[2], arrayOfByte2[3]);
      return arrayOfByte1;
    }

    public static int decode(byte[] paramArrayOfByte, int paramInt, byte paramByte1, byte paramByte2, byte paramByte3, byte paramByte4)
    {
      int i = dec_table[paramByte1];
      int j = dec_table[paramByte2];
      int k = dec_table[paramByte3];
      int m = dec_table[paramByte4];
      if ((i == -1) || (j == -1) || ((k == -1) && (paramByte3 != 61)) || ((m == -1) && (paramByte4 != 61)))
        throw new IllegalArgumentException("Invalid character [" + (paramByte1 & 0xFF) + ", " + (paramByte2 & 0xFF) + ", " + (paramByte3 & 0xFF) + ", " + (paramByte4 & 0xFF) + "]");
      paramArrayOfByte[(paramInt++)] = (byte)(i << 2 | j >>> 4);
      if (paramByte3 == 61)
        return 1;
      paramArrayOfByte[(paramInt++)] = (byte)(j << 4 | k >>> 2);
      if (paramByte4 == 61)
        return 2;
      paramArrayOfByte[(paramInt++)] = (byte)(k << 6 | m);
      return 3;
    }

    public static byte[] encode(byte[] paramArrayOfByte)
    {
      int i = 0;
      int j = 0;
      int k = paramArrayOfByte.length;
      int m = k % 3;
      int n = (k + 2) / 3 * 4 + (k == 0 ? 2 : 0);
      byte[] arrayOfByte = new byte[n];
      int i1;
      int i2;
      for (int i4 = k / 3; i4 > 0; i4--)
      {
        i1 = paramArrayOfByte[(i++)];
        i2 = paramArrayOfByte[(i++)];
        int i3 = paramArrayOfByte[(i++)];
        arrayOfByte[(j++)] = (byte)enc_table[(i1 >>> 2 & 0x3F)];
        arrayOfByte[(j++)] = (byte)enc_table[((i1 << 4 & 0x30) + (i2 >>> 4 & 0xF))];
        arrayOfByte[(j++)] = (byte)enc_table[((i2 << 2 & 0x3C) + (i3 >>> 6 & 0x3))];
        arrayOfByte[(j++)] = (byte)enc_table[(i3 & 0x3F)];
      }
      if (m == 1)
      {
        i1 = paramArrayOfByte[(i++)];
        arrayOfByte[(j++)] = (byte)enc_table[(i1 >>> 2 & 0x3F)];
        arrayOfByte[(j++)] = (byte)enc_table[(i1 << 4 & 0x30)];
        arrayOfByte[(j++)] = 61;
        arrayOfByte[(j++)] = 61;
      }
      else if (m == 2)
      {
        i1 = paramArrayOfByte[(i++)];
        i2 = paramArrayOfByte[(i++)];
        arrayOfByte[(j++)] = (byte)enc_table[(i1 >>> 2 & 0x3F)];
        arrayOfByte[(j++)] = (byte)enc_table[((i1 << 4 & 0x30) + (i2 >>> 4 & 0xF))];
        arrayOfByte[(j++)] = (byte)enc_table[(i2 << 2 & 0x3C)];
        arrayOfByte[(j++)] = 61;
      }
      if (j != n)
        throw new IllegalArgumentException("output length was not the expected");
      return arrayOfByte;
    }
}
